<?php

namespace app\api\controller;

use app\admin\model\shop\Category as CategoryModel;
use app\api\model\Goods as GoodsModel;
use app\api\model\SkuSpec;
use app\common\controller\Api;
use fast\Tree;

/**
 * 商品分类
 */
class Category extends Api
{
    protected $noNeedLogin = ['index', 'alls','lists'];

    public function lists()
    {
        $list = CategoryModel::where('pid', 0)->field('id,name,image')->order('weigh asc,id asc')->select();
        $member_discount = \app\common\model\Config::get(['name'=>'member_discount'])->value;
        foreach ($list as $kk=>$vv){
            $param['category_id'] = $vv['id'];

            $goods_list = GoodsModel::where(function ($query) use ($param) {
                $query->where('status', 'normal');
                //分类
                if (isset($param['category_id']) && !empty($param['category_id'])) {
                    //$query->where('category_id', 'like', '%' . $param['category_id'] . '%');
                    $query->where('category_id', '=',$param['category_id']);
                    //$query->where('category_id', 'IN', \app\admin\model\shop\Category::getCategoryChildrenIds($param['category_id']));
                }
            })->field('id,title,subtitle,marketprice,price,(sales + virtual_sale) as sales,image,stocks')->order("weigh asc")->select();
            foreach ($goods_list as $kkk=>$vvv){
                $row = (new GoodsModel())->with([
                    'Sku',

                ])->where('status', '<>', 'hidden')->where('id', $vvv['id'])->find();


                $row->sku_spec = SkuSpec::getGoodsSkuSpec($vvv['id']);
                $goods_list[$kkk]['sku'] = $row->sku;
                $goods_list[$kkk]['sku_spec'] = $row->sku_spec;

                //if ($this->auth->id&&$this->auth->level==1){
                    $member_price =  $vvv['price']*$member_discount/10;
                    $goods_list[$kkk]['discount_price'] = $member_price>0.01?$member_price:0.01;
                /*} else {
                    $goods_list[$kkk]['discount_price'] = $vvv['price'];
                }*/
            }

            /*if ($this->auth->id&&$this->auth->level==1){
                foreach ($goods_list as $kkk=>$vvv){
                    $goods_list[$kkk]['discount_price'] = $vvv['price']*$member_discount/10;
                }
            } else {
                foreach ($goods_list as $kkk=>$vvv){
                    $goods_list[$kkk]['discount_price'] = $vvv['price'];
                }
            }*/

            $list[$kk]['goods_list'] = $goods_list;
        }
        $this->success('获取成功', $list);
    }

    //分类列表
    public function index()
    {
        $member_discount = \app\common\model\Config::get(['name'=>'member_discount'])->value;
        $tree = Tree::instance();
        $categoryList = CategoryModel::field('id,pid,name,image')->order('weigh asc,id asc')->select();
        $tree->init(collection($categoryList)->toArray(), 'pid');
        $list = $tree->getTreeArray(0);
        foreach ($list as $k=>$v){
            foreach ($v['childlist'] as $kk=>$vv){
                $param['category_id'] = $vv['id'];

                $goods_list = GoodsModel::where(function ($query) use ($param) {
                    $query->where('status', 'normal');
                    //分类
                    if (isset($param['category_id']) && !empty($param['category_id'])) {
                        $query->where('category_id', 'like', '%' . $param['category_id'] . '%');
                        //$query->where('category_id', 'IN', \app\admin\model\shop\Category::getCategoryChildrenIds($param['category_id']));
                    }
                })->field('id,title,subtitle,marketprice,price,image')->order("weigh asc")->select();
                //if ($this->auth->id&&$this->auth->level==1){
                    foreach ($goods_list as $kkk=>$vvv){
                        $member_price =  $vvv['price']*$member_discount/10;
                        $goods_list[$kkk]['discount_price'] = $member_price>0.01?$member_price:0.01;
                    }
                //}

                $list[$k]['childlist'][$kk]['goods_list'] = $goods_list;
            }
        }
        //dump($list);die;
        $this->success('获取成功', $list);
    }

    //所有分类
    public function alls()
    {
        $tree = Tree::instance();
        $list = CategoryModel::field('id,pid,name,image')->order('weigh asc,id asc')->select();
        $tree->init(collection($list)->toArray(), 'pid');
        $categoryList = $tree->getTreeList($tree->getTreeArray(0), 'name');

        $this->success('获取成功', $categoryList);
    }
}
